/*
 * User: pwoodworth
 * Date: Feb 14, 2007
 * Time: 5:50:46 PM
 */
package org.emergent.antbite.tasks;

import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.Project;
import org.apache.tools.ant.Target;
import org.apache.tools.ant.Task;

import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.Set;
import java.util.StringTokenizer;

public class AddTargetDependency extends Task
{
    private String m_targetName;
    private String m_newDeps;
    private String m_extension;
    private String m_targetSuffix = "";

    public void setTargetName( String targetName )
    {
        m_targetName = targetName;
    }

    public void setNewDeps( String newDeps )
    {
        m_newDeps = newDeps;
    }

    public void setExtension( String extension )
    {
        m_extension = extension;
    }

    public void setTargetSuffix( String suffix )
    {
        m_targetSuffix = suffix;
    }

    public void execute() throws BuildException
    {
        if (m_extension == null)
            doNorm();
        else
            doAlt();
    }

    public void doAlt() throws BuildException
    {
        Project project = getProject();
        Hashtable targets = project.getTargets();
        for (Iterator iter = targets.values().iterator(); iter.hasNext(); )
        {
            Target target = (Target)iter.next();
            String targetName = target.getName();
            String deplist = project.getProperty( targetName + m_extension );
            if (deplist != null)
            {
                doNorm( targetName, deplist );
            }
        }
    }

    public void doNorm() throws BuildException
    {
        doNorm( m_targetName, m_newDeps );
    }

    public void doNorm( String targetname, String newdeps ) throws BuildException
    {
        Target metaTarget = getMetaTarget( getProject(), targetname );
        if (metaTarget == null)
            return;
        Set metaDependsSet = new LinkedHashSet();
        Enumeration metaDeps = metaTarget.getDependencies();
        while (metaDeps.hasMoreElements()) {
            String dep = (String)metaDeps.nextElement();
            metaDependsSet.add( dep );
        }

        StringTokenizer tok = new StringTokenizer(newdeps, ",");
        while (tok.hasMoreTokens()) {
            String token = tok.nextToken().trim();
            if (token.length() > 0)
            {
                metaDependsSet.add( token + m_targetSuffix );
            }
        }

        StringBuffer metaDependsBuf = new StringBuffer();
        String[] metadeps = (String[])metaDependsSet.toArray( new String[metaDependsSet.size()] );
        for (int i = 0; i < metadeps.length; i++)
        {
            if (i > 0)
                metaDependsBuf.append( ',' );
            metaDependsBuf.append( metadeps[i] );
        }
        metaTarget.setDepends( metaDependsBuf.toString() );

//        TargetTracker.getInstance( this.getProject() );
    }

    private Target getMetaTarget( Project project, String name ) {
        Target retval = (Target)project.getTargets().get( name );
//        if (retval == null) {
//            retval = new Target();
//            retval.setName( name );
//            retval.setProject( project );
//            project.addTarget( retval );
//        }
        return retval;
    }
}